from flask_sqlalchemy import SQLAlchemy as _SQLAlchemy, BaseQuery
from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, SmallInteger
from contextlib import contextmanager
from datetime import datetime


class SQLAlchemy(_SQLAlchemy):
    @contextmanager
    def auto_commit(self):
        try:
            yield  # 到这里后执行 with下的sql操作代码，然后走下一步
            self.session.commit()
        except Exception as e:
            db.session.rollback()  # 错误回滚
            raise e


class Query(BaseQuery):
    #改写 filter_by
    def filter_by(self, **kwargs):
        if 'status' not in kwargs.keys():
            kwargs['status'] = 1
        return super(Query, self).filter_by(**kwargs)


db = SQLAlchemy(query_class=Query)


class Base(db.Model):
    __abstract__ = True  # 在启动flask时会自动创建 继承了 db.Model 的模型，这个设置是让flask不创建
    create_time = Column('create_time', Integer)
    status = Column(SmallInteger, default=1)

    def __init__(self):
        self.create_time = int(datetime.now().timestamp())

    def set_attrs(self, attrs_dict):
        # 遍历attrs_dict，如果有key和模型的属性（数据库字段）相同，就把这个key的值赋值给模型
        for key, value in attrs_dict.items():
            if hasattr(self, key) and key != 'id':  # 函数判断一个对象是否有某个属性， self 表示当前模型对象
                setattr(self, key, value)  # 对self对象的属性名为key的赋值，值为value。动态赋值

    @property
    def create_datetime(self):
        if self.create_time:
            return datetime.fromtimestamp(self.create_time)
        else:
            return None
